<?php

class ExportController extends Zend_Controller_Action 
{
    protected $_model;
	protected $user;
    
	public function preDispatch(){
		if (Zend_Auth::getInstance()->hasIdentity()) {
			$this->user = Zend_Auth::getInstance()->getIdentity();
            if(!is_object($this->user)){
			    $request = $this->getRequest();
                $c = $request->getControllerName();
                $a = $request->getActionName();
                $loginUrl = $this->view->lang . '/login/index/?returnUrl=';
                $returnUrl = $this->view->url(array('controller' => $c, 'action' => $a), 'default', true);            
                $loginUrl .= $returnUrl;
			    $this->_helper->redirector->gotoUrl($loginUrl);
		    }
		}else{
            $request = $this->getRequest();
            $c = $request->getControllerName();
            $a = $request->getActionName();
            $loginUrl = $this->view->lang . '/login/index/?returnUrl=';
            $returnUrl = $this->view->url(array('controller' => $c, 'action' => $a), 'default', true);            
            $loginUrl .= $returnUrl;
			$this->_helper->redirector->gotoUrl($loginUrl);         
		}
	}
	public function init()
    {
        $ajaxContext = $this->_helper->getHelper('AjaxContext');
        $ajaxContext->addActionContext('view', 'html')
                    ->addActionContext('comment', 'html')                    
                    ->initContext();
    }   
 	
    protected function getPaper()
    {
        if (null === $this->_model) {
            require_once APPLICATION_PATH . '/models/Paper.php';
            $this->_model = new Model_Paper();
        }
        return $this->_model;
    }
	private function getCategories(){
		require_once APPLICATION_PATH . '/models/Category.php';
      	$model = new Model_Category();
      	return $model->fetchEntries();
    } 
	private function getReview(){
		require_once APPLICATION_PATH . '/models/Review.php';
      	$model = new Model_Review();
      	return $model;
    }     
	private function getAssignment(){
		require_once APPLICATION_PATH . '/models/Assignment.php';
      	$model = new Model_Assignment();
      	return $model;
    }
    private function getAreas(){
		require_once APPLICATION_PATH . '/models/Area.php';
      	$model = new Model_Area();
      	return $model->fetchEntries();
    } 
    private function getActiveConference(){
		require_once APPLICATION_PATH . '/models/Conference.php';
      	$model = new Model_Conference();
      	return $model->getActive();
    }   
    
    // Functions for export to excel.
    function xlsBOF() {
        echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
        return;
    }
    function xlsEOF() {
        echo pack("ss", 0x0A, 0x00);
        return;
    }
    function xlsWriteNumber($Row, $Col, $Value) {
        echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
        echo pack("d", $Value);
        return;
    }
    function xlsWriteLabel($Row, $Col, $Value ) {
        $L = strlen($Value);
        echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
        echo $Value;
        return;
    }
    protected function getParticipant(){        
        require_once APPLICATION_PATH . '/models/Participant.php';
        $p = new Model_Participant();        
        return $p;
    }
    function papersAction(){    
        $table = $this->getPaper();
        $papers = $table->getAll('id', 'asc');
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Content-Type: application/force-download;");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");;
        header("Content-Disposition: attachment;filename=papers.xls ");
        header("Content-Transfer-Encoding: binary ");        
        
        $this->xlsBOF();             
        $this->xlsWriteLabel(0, 0, 'ID');            
        $this->xlsWriteLabel(0, 1, 'Title');            
        $this->xlsWriteLabel(0, 2, 'Authors');            
        $this->xlsWriteLabel(0, 3, 'Abstract');            
        $this->xlsWriteLabel(0, 4, 'Area');            
        $this->xlsWriteLabel(0, 5, 'Category');            
        $this->xlsWriteLabel(0, 6, 'Date Sent');                    
        $this->xlsWriteLabel(0, 7, 'Status');            
        $this->xlsWriteLabel(0, 8, 'EMail');                    
        $xlsRow = 1;
        
        foreach($papers as $p){       
            $this->xlsWriteNumber($xlsRow, 0, $p['id']);            
            $this->xlsWriteLabel($xlsRow, 1, $p['title']);            
            $this->xlsWriteLabel($xlsRow, 2, $p['authors']);
            $this->xlsWriteLabel($xlsRow, 3, $p['abstract']);
            $this->xlsWriteLabel($xlsRow, 4, $p['area_name']);
            $this->xlsWriteLabel($xlsRow, 5, $p['category_name']);
            $this->xlsWriteLabel($xlsRow, 6, $p['date_sent']);            
            $this->xlsWriteLabel($xlsRow, 7, $p['status_name']);
            $this->xlsWriteLabel($xlsRow, 8, $p['email']);  
            $xlsRow++;
        }
        
        $this->xlsEOF();
        exit();
    }
    
    function participantsAction(){
    
        $table = $this->getParticipant();
        $participants = $table->fetchEntries();
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Content-Type: application/force-download;");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");;
        header("Content-Disposition: attachment;filename=participants.xls ");
        header("Content-Transfer-Encoding: binary ");        
        
        $this->xlsBOF();             
        $this->xlsWriteLabel(0, 0, 'ID');            
        $this->xlsWriteLabel(0, 1, 'First Name');            
        $this->xlsWriteLabel(0, 2, 'Last Name');            
        $this->xlsWriteLabel(0, 3, 'Institution');            
        $this->xlsWriteLabel(0, 4, 'EMail');            
        $this->xlsWriteLabel(0, 5, 'Reviewer');            
        
        $xlsRow = 1;
        
        foreach($participants as $p){       
            $this->xlsWriteNumber($xlsRow, 0, $p['id']);            
            $this->xlsWriteLabel($xlsRow, 1, $p['first_name']);            
            $this->xlsWriteLabel($xlsRow, 2, $p['last_name']);
            $this->xlsWriteLabel($xlsRow, 3, $p['institution']);
            $this->xlsWriteLabel($xlsRow, 4, $p['email']);
            $this->xlsWriteLabel($xlsRow, 5, $p['isReviewer']);            
            $xlsRow++;
        }
        
        $this->xlsEOF();
        exit();
    }
    function participants1Action(){
    
        $table = $this->getParticipant();
        $participants = $table->fetchEntries();
        header("Pragma: public");
        header("Expires: 0");        
        header("Content-Type: text/html; charset=utf-8");        
        
        $res = '';        
        $res .= 'ID, ';
        $res .= 'First Name, ';
        $res .= 'Last Name, ';
        $res .= 'Institution, ';
        $res .= 'Email, ';
        $res .= 'Is Reviewer\n\r';            
            
        foreach($participants as $p){       
            $res .= $p['id'] . ', ';
            $res .= $p['first_name'] . ', ';
            $res .= $p['last_name'] . ', ';
            $res .= $p['institution'] . ', ';
            $res .= $p['email'] . ', ';
            $res .= $p['isReviewer'] . '\n\r';            
        }
        echo $res;        
        exit();
    }
}
